package dom.rose.controller;



import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.rose.entity.Dept;
import dom.rose.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @HystrixCommand(fallbackMethod = "hystrixGet")
    @GetMapping("/dept/get/{id}")
    public Dept selectByPrimaryKey(@PathVariable("id") Long deptno) {
        Dept dept = deptService.selectByPrimaryKey(deptno);
        if (dept == null){
            throw new RuntimeException("id=>"+deptno+"这个id不存在");
        }
        return dept;
    }

    // 备选方案
//    @GetMapping("/dept/get/{id}")  不能写这个
    public Dept hystrixGet(@PathVariable("id") Long deptno) {

        return new Dept().setDeptno(deptno).setDname("id=>"+deptno+"，不存在该用户,这个id不存在--@hystrix").setDbSource("无数据");
    }















    @PostMapping("/dept/add")
    public int addDept(Dept record){
        return deptService.insert(record);
    }



    @GetMapping("/dept/list")
    public List<Dept> queryAll() {
        return deptService.queryAll();
    }






}
